/**
 * 任务平台状态字典
 * 包含任务状态、接单人状态、发布方状态及对应的操作按钮
 */

// 任务状态枚举
export const TASK_STATUS = {
  PAID: 1,           // 已支付
  IN_PROGRESS: 2,    // 进行中
  CANCELLED: 3,      // 已取消
  COMPLETED: 4,      // 已完结
  DISPUTED: 5,       // 有纠纷
} as const;

// 接单人状态枚举
export const APPLICANT_STATUS = {
  APPLIED: 10,       // 已报名
  SELECTED: 11,      // 已中标
  REJECTED: 12,      // 已拒绝
  WORKING: 13,       // 进行中
  SUBMITTED: 14,     // 已完成
  DISPUTED: 15,      // 有纠纷
} as const;

// 发布方状态枚举
export const PUBLISHER_STATUS = {
  PUBLISHED: 20,     // 已发布
  CONFIRMED: 21,     // 已确认中标者
  SETTLED: 22,       // 已结算
  DISPUTED: 23,      // 有纠纷
} as const;

// 用户角色枚举
export const USER_ROLE = {
  PUBLISHER: 'publisher', // 发布方
  APPLICANT: 'applicant', // 接单人
} as const;

// 操作按钮类型枚举
export const ACTION_TYPE = {
  // 发布方操作
  CANCEL_TASK: 'cancel_task',           // 取消任务
  VIEW_APPLICANTS: 'view_applicants',   // 查看报名列表
  SELECT_WINNER: 'select_winner',       // 选择中标者
  VIEW_PROGRESS: 'view_progress',       // 查看任务进度
  CONFIRM_COMPLETION: 'confirm_completion', // 确认完成
  RAISE_DISPUTE: 'raise_dispute',       // 提出纠纷
  COMMUNICATE_PLATFORM: 'communicate_platform', // 与平台沟通
  ACCEPT_SOLUTION: 'accept_solution',   // 接受解决方案
  REJECT_SOLUTION: 'reject_solution',   // 拒绝解决方案
  DELETE_TASK: 'delete_task',           // 删除任务
  
  // 接单人操作
  APPLY_TASK: 'apply_task',             // 报名任务
  CANCEL_APPLICATION: 'cancel_application', // 取消报名
  START_TASK: 'start_task',             // 开始任务
  ABANDON_TASK: 'abandon_task',         // 放弃任务
  SUBMIT_COMPLETION: 'submit_completion', // 提交完成
  WAIT_CONFIRMATION: 'wait_confirmation', // 等待确认
} as const;

// 类型定义
export type TaskStatus = typeof TASK_STATUS[keyof typeof TASK_STATUS];
export type ApplicantStatus = typeof APPLICANT_STATUS[keyof typeof APPLICANT_STATUS];
export type PublisherStatus = typeof PUBLISHER_STATUS[keyof typeof PUBLISHER_STATUS];
export type UserRole = typeof USER_ROLE[keyof typeof USER_ROLE];
export type ActionType = typeof ACTION_TYPE[keyof typeof ACTION_TYPE];

// 状态配置接口
interface StatusConfig {
  label: string;
  color: string;
  bgColor: string;
  borderColor: string;
  publisherActions?: ActionType[];
  applicantActions?: ActionType[];
  actions?: ActionType[];
}

// 操作按钮配置接口
interface ActionConfig {
  label: string;
  type: 'primary' | 'danger' | 'warning' | 'info' | 'default' | 'success';
  size: 'small' | 'normal' | 'large';
  icon: string;
  disabled?: boolean;
}

// 状态配置对象
export const STATUS_CONFIG: Record<number, StatusConfig> = {
  // 任务状态配置
  [TASK_STATUS.PAID]: {
    label: '已支付',
    color: '#52c41a',
    bgColor: '#f6ffed',
    borderColor: '#b7eb8f',
    publisherActions: [
      // ACTION_TYPE.CANCEL_TASK,
      ACTION_TYPE.VIEW_APPLICANTS,
      // ACTION_TYPE.SELECT_WINNER,
    ],
    applicantActions: [
      ACTION_TYPE.APPLY_TASK,
      ACTION_TYPE.CANCEL_APPLICATION,
    ],
  },
  [TASK_STATUS.IN_PROGRESS]: {
    label: '进行中',
    color: '#1890ff',
    bgColor: '#e6f7ff',
    borderColor: '#91d5ff',
    publisherActions: [
      ACTION_TYPE.VIEW_PROGRESS,
      ACTION_TYPE.CONFIRM_COMPLETION,
      ACTION_TYPE.RAISE_DISPUTE,
    ],
    applicantActions: [
      ACTION_TYPE.SUBMIT_COMPLETION,
      ACTION_TYPE.RAISE_DISPUTE,
    ],
  },
  [TASK_STATUS.CANCELLED]: {
    label: '已取消',
    color: '#999999',
    bgColor: '#f5f5f5',
    borderColor: '#d9d9d9',
    publisherActions: [ACTION_TYPE.DELETE_TASK],
    applicantActions: [],
  },
  [TASK_STATUS.COMPLETED]: {
    label: '已完结',
    color: '#722ed1',
    bgColor: '#f9f0ff',
    borderColor: '#d3adf7',
    publisherActions: [],
    applicantActions: [],
  },
  [TASK_STATUS.DISPUTED]: {
    label: '有纠纷',
    color: '#fa541c',
    bgColor: '#fff2e8',
    borderColor: '#ffbb96',
    publisherActions: [
      ACTION_TYPE.COMMUNICATE_PLATFORM,
      ACTION_TYPE.ACCEPT_SOLUTION,
      ACTION_TYPE.REJECT_SOLUTION,
    ],
    applicantActions: [
      ACTION_TYPE.COMMUNICATE_PLATFORM,
      ACTION_TYPE.ACCEPT_SOLUTION,
      ACTION_TYPE.REJECT_SOLUTION,
    ],
  },
  
  // 接单人状态配置
  [APPLICANT_STATUS.APPLIED]: {
    label: '已报名',
    color: '#1890ff',
    bgColor: '#e6f7ff',
    borderColor: '#91d5ff',
    actions: [ACTION_TYPE.CANCEL_APPLICATION],
  },
  [APPLICANT_STATUS.SELECTED]: {
    label: '已中标',
    color: '#52c41a',
    bgColor: '#f6ffed',
    borderColor: '#b7eb8f',
    actions: [ACTION_TYPE.START_TASK, ACTION_TYPE.ABANDON_TASK],
  },
  [APPLICANT_STATUS.REJECTED]: {
    label: '已拒绝',
    color: '#ff4d4f',
    bgColor: '#fff2f0',
    borderColor: '#ffccc7',
    actions: [],
  },
  [APPLICANT_STATUS.WORKING]: {
    label: '进行中',
    color: '#1890ff',
    bgColor: '#e6f7ff',
    borderColor: '#91d5ff',
    actions: [ACTION_TYPE.SUBMIT_COMPLETION, ACTION_TYPE.RAISE_DISPUTE],
  },
  [APPLICANT_STATUS.SUBMITTED]: {
    label: '已完成',
    color: '#722ed1',
    bgColor: '#f9f0ff',
    borderColor: '#d3adf7',
    actions: [ACTION_TYPE.WAIT_CONFIRMATION, ACTION_TYPE.RAISE_DISPUTE],
  },
  [APPLICANT_STATUS.DISPUTED]: {
    label: '有纠纷',
    color: '#fa541c',
    bgColor: '#fff2e8',
    borderColor: '#ffbb96',
    actions: [
      ACTION_TYPE.COMMUNICATE_PLATFORM,
      ACTION_TYPE.ACCEPT_SOLUTION,
      ACTION_TYPE.REJECT_SOLUTION,
    ],
  },
  
  // 发布方状态配置
  [PUBLISHER_STATUS.PUBLISHED]: {
    label: '已发布',
    color: '#52c41a',
    bgColor: '#f6ffed',
    borderColor: '#b7eb8f',
    actions: [
      // ACTION_TYPE.CANCEL_TASK,
      ACTION_TYPE.VIEW_APPLICANTS,
      // ACTION_TYPE.SELECT_WINNER,
    ],
  },
  [PUBLISHER_STATUS.CONFIRMED]: {
    label: '已确认中标者',
    color: '#1890ff',
    bgColor: '#e6f7ff',
    borderColor: '#91d5ff',
    actions: [
      ACTION_TYPE.VIEW_PROGRESS,
      ACTION_TYPE.CONFIRM_COMPLETION,
      ACTION_TYPE.RAISE_DISPUTE,
    ],
  },
  [PUBLISHER_STATUS.SETTLED]: {
    label: '已结算',
    color: '#722ed1',
    bgColor: '#f9f0ff',
    borderColor: '#d3adf7',
    actions: [],
  },
  [PUBLISHER_STATUS.DISPUTED]: {
    label: '有纠纷',
    color: '#fa541c',
    bgColor: '#fff2e8',
    borderColor: '#ffbb96',
    actions: [
      ACTION_TYPE.COMMUNICATE_PLATFORM,
      ACTION_TYPE.ACCEPT_SOLUTION,
      ACTION_TYPE.REJECT_SOLUTION,
    ],
  },
};

// 操作按钮配置
export const ACTION_CONFIG: Record<ActionType, ActionConfig> = {
  [ACTION_TYPE.CANCEL_TASK]: {
    label: '取消任务',
    type: 'danger',
    size: 'small',
    icon: 'close',
  },
  [ACTION_TYPE.VIEW_APPLICANTS]: {
    label: '查看申请者',
    type: 'info',
    size: 'small',
    icon: 'people',
  },
  [ACTION_TYPE.SELECT_WINNER]: {
    label: '选择中标者',
    type: 'primary',
    size: 'small',
    icon: 'check',
  },
  [ACTION_TYPE.VIEW_PROGRESS]: {
    label: '查看进度',
    type: 'info',
    size: 'small',
    icon: 'eye',
  },
  [ACTION_TYPE.CONFIRM_COMPLETION]: {
    label: '确认完成',
    type: 'success',
    size: 'small',
    icon: 'check-circle',
  },
  [ACTION_TYPE.RAISE_DISPUTE]: {
    label: '提出纠纷',
    type: 'warning',
    size: 'small',
    icon: 'warning',
  },
  [ACTION_TYPE.COMMUNICATE_PLATFORM]: {
    label: '联系平台',
    type: 'info',
    size: 'small',
    icon: 'service',
  },
  [ACTION_TYPE.ACCEPT_SOLUTION]: {
    label: '接受方案',
    type: 'success',
    size: 'small',
    icon: 'check',
  },
  [ACTION_TYPE.REJECT_SOLUTION]: {
    label: '拒绝方案',
    type: 'danger',
    size: 'small',
    icon: 'close',
  },
  [ACTION_TYPE.DELETE_TASK]: {
    label: '删除任务',
    type: 'danger',
    size: 'small',
    icon: 'delete',
  },
  [ACTION_TYPE.APPLY_TASK]: {
    label: '报名任务',
    type: 'primary',
    size: 'small',
    icon: 'add',
  },
  [ACTION_TYPE.CANCEL_APPLICATION]: {
    label: '取消报名',
    type: 'default',
    size: 'small',
    icon: 'close',
  },
  [ACTION_TYPE.START_TASK]: {
    label: '开始任务',
    type: 'primary',
    size: 'small',
    icon: 'play',
  },
  [ACTION_TYPE.ABANDON_TASK]: {
    label: '放弃任务',
    type: 'warning',
    size: 'small',
    icon: 'stop',
  },
  [ACTION_TYPE.SUBMIT_COMPLETION]: {
    label: '提交完成',
    type: 'success',
    size: 'small',
    icon: 'upload',
  },
  [ACTION_TYPE.WAIT_CONFIRMATION]: {
    label: '等待确认',
    type: 'default',
    size: 'small',
    icon: 'clock',
    disabled: true,
  },
};

/**
 * 获取状态配置
 * @param status - 状态值
 * @param statusType - 状态类型 ('task', 'applicant', 'publisher')
 * @returns 状态配置对象
 */
export const getStatusConfig = (status: number, statusType: 'task' | 'applicant' | 'publisher' = 'task'): StatusConfig | null => {
  const statusMap = {
    task: TASK_STATUS,
    applicant: APPLICANT_STATUS,
    publisher: PUBLISHER_STATUS,
  };
  
  const configKey = Object.keys(statusMap[statusType]).find(
    key => statusMap[statusType][key as keyof typeof statusMap[typeof statusType]] === status
  );
  
  return configKey ? STATUS_CONFIG[status] || null : null;
};

/**
 * 获取用户在当前状态下的可用操作
 * @param taskStatus - 任务状态
 * @param userRole - 用户角色 ('publisher' | 'applicant')
 * @param userStatus - 用户状态（接单人状态或发布方状态）
 * @returns 可用操作列表
 */
export const getAvailableActions = (taskStatus: number, userRole: UserRole, userStatus?: number): ActionType[] => {
  const taskConfig = STATUS_CONFIG[taskStatus];
  if (!taskConfig) return [];
  
  if (userRole === USER_ROLE.PUBLISHER) {
    // 如果有用户状态，优先使用用户状态的操作
    if (userStatus && STATUS_CONFIG[userStatus]) {
      return STATUS_CONFIG[userStatus].actions || [];
    }
    return taskConfig.publisherActions || [];
  } else if (userRole === USER_ROLE.APPLICANT) {
    // 如果有用户状态，优先使用用户状态的操作
    if (userStatus && STATUS_CONFIG[userStatus]) {
      return STATUS_CONFIG[userStatus].actions || [];
    }
    return taskConfig.applicantActions || [];
  }
  
  return [];
};

/**
 * 获取操作按钮配置
 * @param actionType - 操作类型
 * @returns 按钮配置对象
 */
export const getActionConfig = (actionType: ActionType): ActionConfig => {
  return ACTION_CONFIG[actionType] || {} as ActionConfig;
};
